/*
app顶部tabs层
 */

import 'package:flutter/material.dart';
import 'Home.dart';
import 'Category.dart';
import 'Cart.dart';
import 'User.dart';
import '../../services/ScreenAdapter.dart';

class Tabs extends StatefulWidget {
  Tabs({Key key}) : super(key: key);

  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {

  //使用 AutomaticKeepAliveClientMixin 来保持页面的持久化
  PageController _pageController;
  void initState(){
  //页面控制器初始化
    _pageController = new PageController(initialPage : _currentIndex);
    super.initState();
  }
  int _currentIndex=0;

  List<Widget> _pageList=[
    HomePage(),
    CategoryPage(),
    CartPage(),
    UserPage()
  ];

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      appBar:this._currentIndex!=3?AppBar(
        leading: IconButton(
          icon: Icon(Icons.center_focus_weak, size: 28, color: Colors.black87),
          onPressed: null,
        ),
        title: InkWell(
          //放一个输入框直接跳转
          child: Container(
            height: ScreenAdapter.height(68),
            decoration: BoxDecoration(
                color: Color.fromRGBO(233, 233, 233, 0.8),
                borderRadius: BorderRadius.circular(30)
            ),
            padding: EdgeInsets.only(left: 12),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Icon(Icons.search),
                Text(" 笔记本",style: TextStyle(
                    fontSize: ScreenAdapter.size(28)
                ))
              ],
            ),
          ),
          onTap: (){
            Navigator.pushNamed(context, '/search');
          },
        ),

        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.message, size: 28, color: Colors.black87),
            onPressed: null,
          )
        ],
      ):AppBar(
        title: Text("用户中心"),
      ),


      //1、body默认写法
      // body: this._pageList[this._currentIndex],

      //2、indexedStack 写法 保持页面状态
      // body:new IndexedStack(
      //   index:this._currentIndex,
      //   children: this._pageList,
      // ),

      //3、AutomaticKeepAliveClientMixin保持页面持久化 可选让页面是否动态还是保持 (必须用 PageView 加载不同的页面)
      body: new PageView(
        controller: this._pageController,//默认页面
        children: this._pageList,
        // physics: NeverScrollableScrollPhysics(), //禁止pageView滑动
        onPageChanged: (index){
          setState(() {
            this._currentIndex = index;
          });
        },
      ),

      //底部icon图标
      bottomNavigationBar: BottomNavigationBar(
        currentIndex:this._currentIndex ,
        onTap: (index){
          setState(() {
            this._currentIndex=index;

            //配合AutomaticKeepAliveClientMixin 跳转页面
            _pageController.jumpToPage(index);

          });
        },
        type:BottomNavigationBarType.fixed ,
        fixedColor:Colors.red,
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label:"首页"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.category),
              label:"分类"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_cart),
              label:"购物车"
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.people),
              label:"我的"
          )
        ],
      ),

    );
  }
}